//
//  VSNotificaitonsManager.m
//  VeSync
//
//  Created by Etekcity on 2019/5/14.
//  Copyright © 2019 Etekcity. All rights reserved.
//

#import "VSNotificaitonsManager.h"
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif
#import <UIKit/UIKit.h>
#define LocalNotiReqIdentifer    @"LocalNotiReqIdentifer"

@implementation VSNotificaitonsMode



@end


@implementation VSNotificaitonsManager


+ (instancetype) manager{
    static VSNotificaitonsManager * _manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_manager) {
            _manager = [[self alloc] init];
        }
    });
    return _manager;
}

- (void)sendLocalNotification:(VSNotificaitonsMode *)mode {
    
    
    NSString *title = mode.title? : @"";
    NSString *body = mode.body? : @"";
    NSInteger badge = mode.bage;
    NSInteger timeInteval = 1;
    NSDictionary *userInfo = mode.userInfo ? : @{};
    if (@available(iOS 10.0, *)) {
        
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.sound = [UNNotificationSound defaultSound];
        content.title = title;
        content.body = body;
        content.badge = @(badge);
        content.userInfo = userInfo;
        
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInteval repeats:NO];
        
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:LocalNotiReqIdentifer content:content trigger:trigger];
        
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        }];
        
    } else {
        
                UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        
                localNotification.alertBody = body;
                localNotification.alertTitle = title;
        
                localNotification.soundName = UILocalNotificationDefaultSoundName;// UILocalNotificationDefaultSoundName;
        
                localNotification.userInfo = userInfo;
        
                [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }
}


- (void)cancelLocalNotificaitons {
    
    //! 取消一个特定的通知
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //获取当前所有的本地通知
    if (!notificaitons || notificaitons.count <= 0) { return; }
    for (UILocalNotification *notify in notificaitons) {
        if ([[notify.userInfo objectForKey:@"id"] isEqualToString:@"LOCAL_NOTIFY_SCHEDULE_ID"]) {
            if (@available(iOS 10.0, *)) {
                [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[LocalNotiReqIdentifer]];
            } else {
                [[UIApplication sharedApplication] cancelLocalNotification:notify];
            }
            break;
        }
    }
    //! 取消所有的本地通知
    //[[UIApplication sharedApplication] cancelAllLocalNotifications];
}

@end
